import {createRouter, createWebHashHistory} from "vue-router";
import {Session} from "/@utils/StorageTools";

import {useUserConfig} from "/@bean/GlobalConfig";


// 静态路由
const router = createRouter({
    history : createWebHashHistory(),
    routes: [
        {
            name:'MainIndex',
            path:'/',
            // @ts-ignore
            component:()=>import('/@page/bigScreen/BigScreen.vue'),
            meta:{
                title:'首页',
                keepAlive:true
            },
            children:[
                {
                    name:'userList',
                    path:'/admin/UserList',
                    // @ts-ignore
                    component:()=>import('/@page/admin/UserList.vue'),
                    meta:{
                        title:'用户列表',
                        keepAlive:true
                    }
                },
                {
                    name:'UserInfoPage',
                    path:'/userInfo',
                    // @ts-ignore
                    component:()=>import('/@page/user/UserInfoPage.vue')
                },
                {
                    name:'ChangePasswordPage',
                    path:'/changePassword',
                    // @ts-ignore
                    component:()=>import('/@page/user/ChangePasswordPage.vue')
                }
            ]
        },
        {
            name:'LoginPage',
            path:'/login',
            // @ts-ignore
            component:()=>import('/@page/login/LoginIndex.vue')
        },
        {
            name:'BigScreenPage',
            path:'/bigScreen',
            // @ts-ignore
            component:()=>import('/@page/bigScreen/BigScreen.vue')
        },
        // {
        //     name:'BigScreenPageEdit',
        //     path:'/bigScreenEdit',
        //     // @ts-ignore
        //     component:()=>import('/@page/bigScreen/BigScreenEdit.vue')
        // }
    ]
})


// 登录拦截
router.beforeEach((to, from, next):void => {
    const useUser = useUserConfig();
    const { userInfo } = storeToRefs(useUser);
    // 正则匹配 以 /demo 开头的路由 或者 /login /register 及/ 本身 无需登录即可访问
    if (to.path.match(/^\/demo/) || to.path === '/login' || to.path === '/register') {
        next()
    }
    else {
        // let token = userInfo.value.token;
        // if (token === null) {
        //     next('/login')
        // } else {
        //     next()
        // }
        next()
    }
})

export default router
